<template>
    <el-dialog title="设备选择" v-model="visible" width="800px" top="5vh" append-to-body>
        <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="68px">
         <el-form-item label="设备名称" prop="deviceName">
            <el-input
               v-model="queryParams.deviceName"
               placeholder="请输入设备名称"
               clearable
               style="width: 240px"
            />
         </el-form-item>
        <el-form-item label="资产编码" prop="deviceNo">
            <el-input
               v-model="queryParams.deviceNo"
               placeholder="请输入资产编码"
               clearable
               style="width: 240px"
            />
         </el-form-item>
        <el-form-item label="申请科室" prop="applyDept">
            <el-input
               v-model="queryParams.applyDept"
               placeholder="请输入申请科室"
               clearable
               style="width: 240px"
            />
         </el-form-item>
        <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>
      
      <div>
        已选配件编码：
        <span v-for="(item, index) in sectionDevices" :key="index"><el-tag> {{ item }} </el-tag></span>
      </div>
      <!-- 表格数据 -->
      <el-table v-loading="loading" :data="deviceList" ref="tableRef" @selection-change="handleSelectionChange" row-key="deviceNo">
        <el-table-column
                type="selection"
                :reserve-selection="true"
                width="55">
        </el-table-column>
        <el-table-column label="设备名称" prop="deviceName"  />
         <el-table-column label="资产编码" prop="deviceNo"  />
         <el-table-column label="科室名称" prop="deviceSectionName"  />
         <el-table-column label="购置金额" prop="purchaseMoney"  />
      </el-table>
      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNo"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />
        <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
    </el-dialog>
</template>

<script setup name="deviceChoose2">
import {listDevice} from "@/api/system/device"
const router = useRouter()
const { proxy } = getCurrentInstance()
const visible = ref(false)
const showSearch = ref(true)
const loading = ref(false)
const deviceList = ref([])
const total = ref(0)
const selectedRows = ref([])
const tableRef = ref(null);

const sectionDevices = ref([])
//const selectedRows = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 5
  },
  rules: {}
})
const { queryParams, form, rules } = toRefs(data)

/** 查询列表 */
function getList() {
    loading.value = true
    listDevice(queryParams.value).then(response => {
        deviceList.value = response.result.records
        total.value = response.result.total
        loading.value = false
        nextTick(() => {
            deviceList.value.forEach((row) => {
                debugger
                if (selectedRows.value.some(selected => selected.deviceNo === row.deviceNo)) {
                    tableRef.value?.toggleRowSelection(row, true)
                }
            });
        })
  })
}
function handleQuery(){
  queryParams.value.pageNo = 1
  getList()
}
function resetQuery(){}

function submitForm(){
    proxy.$emit('data-from-child', sectionDevices.value);
    visible.value = false
}

function cancel(){
      visible.value = false
}
// 显示弹框
function show(row) {
  reset()
  visible.value = true
  const mockSelectedRows = []
  if (row.scrapPartsDeviceNos) {
    row.scrapPartsDeviceNos.split(",").map(item => {
    mockSelectedRows.push({ deviceNo: item})
    sectionDevices.value=row.scrapPartsDeviceNos.split(",")
    })
    selectedRows.value = mockSelectedRows
  }   
  getList()
}
function reset() {
    selectedRows.value = []
    sectionDevices.value=[]
    tableRef.value?.clearSelection();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
    debugger
    selectedRows.value = selection
    sectionDevices.value = selection.map(item => item.deviceNo)
}

defineExpose({
  show,
})
</script>